Skip to main content

Python JSON

banner-python

๐Ÿง  How to Read JSON in Python Without Losing Your Sanity (or Your Data)โ€‹

Welcome to the world of json.load() โ€“ your best pal when it comes to unboxing JSON files into shiny Python objects! Whether youโ€™re building apps, scraping data, or just trying to figure out what that mysterious data.json file actually holdsโ€ฆ this guide is for YOU.

Buckle up. It's about to get deserializational ๐Ÿ˜Ž


๐Ÿ” TL;DR โ€“ Show Me the Codeโ€‹

If youโ€™re the kind of person who opens gifts before reading the card, hereโ€™s your quick-start gift:

import json

with open('file_dir_location/data.json') as fp:
data = json.load(fp)

# The data is of type 'dict'
print(data)

๐Ÿ‘† Boom! You just read a JSON file and converted it into a Python object like a pro. ๐ŸŽฉโœจ


๐Ÿงฌ 1. What on Earth is json.load()?โ€‹

Glad you asked! json.load() is like your bilingual buddy who reads JSON and translates it into Python.

๐ŸŽ“ Translation Guide: JSON to Pythonโ€‹

JSONPython
objectdict
arraylist
stringstr
number (int)int
number (real)float
trueTrue
falseFalse
nullNone

Basically, json.load() turns this:

{"name": "AI Bot", "age": 3, "isSmart": true}

Into this:

{'name': 'AI Bot', 'age': 3, 'isSmart': True}

Itโ€™s like magicโ€ฆ but better, because itโ€™s Pythonic.


๐Ÿงช 2. Reading JSON Files โ€“ Letโ€™s Get Our Hands Dirtyโ€‹

๐Ÿงพ Example 1: Reading a JSON File from the Filesystemโ€‹

Suppose you have a file named users.json that looks like this:

[
{
"id": 1,
"name": "Sujit",
"username": "Sujit",
"email": "Sujit@gmail.com"
},
{
"id": 2,
"name": "Brian",
"username": "brian",
"email": "brian@gmail.com"
}
]

Your Python script can read it like this:

import json

with open('users.json') as fp:
data = json.load(fp)

# list
print(type(data))

# Verify read data
print(data)

๐Ÿ–จ๏ธ Program output:

<class 'list'>

[{'id': 1, 'name': 'Sujit', 'username': 'Sujit', 'email': 'Sujit@gmail.com'},
{'id': 2, 'name': 'Brian', 'username': 'brian', 'email': 'brian@gmail.com'}]

๐Ÿ“Œ Note: The root of the JSON is an array, so Python loads it as a list of dictionaries. Itโ€™s like a digital address book, but without the annoying paper cuts.


๐ŸŒ Example 2: Reading a JSON File from the Interwebsโ€‹

Because sometimes your data lives far, far away (in the Cloud โ˜๏ธ).

We'll use the trusty requests library โ€” think of it as your HTTP butler.

import json
import requests

response = requests.get("https://jsonplaceholder.typicode.com/users")
users = json.loads(response.text)

print(users)

๐ŸŽ Program output: We wonโ€™t spoil the surprise here. Run the program to see a list of 10 beautiful JSON objectsโ€”each representing a fake user.

But hereโ€™s a sneak peek:

[{'id': 1, 'name': 'Leanne Graham', 'username': 'Bret', 'email': 'Sincere@april.biz'}, ... ]

Yes, itโ€™s fake data, but hey, it works great for testing and learning!


๐ŸŽ‰ Conclusion: Go Forth and Deserializeโ€‹

Now you know how to:

  • Read JSON from a file like a code wizard ๐Ÿง™
  • Talk to web servers and grab JSON from URLs ๐Ÿ“ก
  • Transform raw JSON into structured Python data ๐Ÿ

So next time someone says "can you parse this JSON?", you can smile smugly and say:

โ€œAbsolutely. Would you like that loaded or pretty-printed?โ€

Happy Learning & Stay JSON-tastic! ๐Ÿ’พโœจ